/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.utilities;

import com.kimstore.dao.GenericJpaDao;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.swing.JComboBox;
import javax.swing.RowFilter;
import javax.swing.RowSorter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.RowSorterEvent;
import javax.swing.event.RowSorterEvent.Type;
import javax.swing.event.RowSorterListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.swingx.JXSearchField;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.sort.RowFilters;

/**
 *
 * @author Administrator
 */
public class TableSearchAndFilterHandler implements RowSorterListener, ListSelectionListener {

    private JXTable table;
    private ArrayList<JXSearchField> searchFields = new ArrayList<JXSearchField>();
    private ArrayList<int[]> searchFieldsColumns = new ArrayList<int[]>();
    private ArrayList<JComboBox> filterComboBoxes = new ArrayList<JComboBox>();
    private ArrayList<int[]> filterComboBoxesColumns = new ArrayList<int[]>();
    private GenericJpaDao dao;

    public void addSearchField(JXSearchField searchField, String prompt, int... columns) {
        searchField.setPrompt(prompt);
        searchFields.add(searchField);
        searchFieldsColumns.add(columns);
    }

    public void addFilterComboBoxes(JComboBox filterComboBox, int... columns) {
        filterComboBoxes.add(filterComboBox);
        filterComboBoxesColumns.add(columns);
    }

    public void bindElements() {
        for (final JXSearchField jsf : searchFields) {
            jsf.setCancelAction(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    jsf.setText("");
                    filterTable();
                }
            });

            jsf.addKeyListener(new KeyAdapter() {
                @Override
                public void keyReleased(KeyEvent evt) {
                    filterTable();
                }
            });
        }

        for (JComboBox jcb : filterComboBoxes) {
            if (jcb != null) {
                jcb.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        filterTable();
                    }
                });
            }
        }
    }

    private void filterTable() {
        List filters = new ArrayList();

        for (int i = 0; i < searchFields.size(); i++) {
            JXSearchField jsf = searchFields.get(i);

            Pattern searchFieldPattern = Pattern.compile(jsf.getText() + ".*", Pattern.CASE_INSENSITIVE);
            filters.add(RowFilters.regexFilter(searchFieldPattern, this.searchFieldsColumns.get(i)));
        }
        for (int i = 0; i < filterComboBoxes.size(); i++) {
            JComboBox jcb = filterComboBoxes.get(i);
            if (jcb != null) {
                if (!jcb.getSelectedItem().toString().equals("All")) {
                    Pattern filterComboBoxPattern = Pattern.compile(jcb.getSelectedItem().toString() + ".*", Pattern.CASE_INSENSITIVE);
                    filters.add(RowFilters.regexFilter(filterComboBoxPattern, this.filterComboBoxesColumns.get(i)));
                }
            }
        }

        table.setRowFilter(RowFilter.andFilter(filters));
    }

    public JXTable getTable() {
        return table;
    }

    public void setTable(JXTable table) {
        this.table = table;
    }

    public ArrayList<JXSearchField> getSearchFields() {
        return searchFields;
    }

    public void setSearchFields(ArrayList<JXSearchField> searchFields) {
        this.searchFields = searchFields;
    }

    public ArrayList<int[]> getSearchFieldsColumns() {
        return searchFieldsColumns;
    }

    public void setSearchFieldsColumns(ArrayList<int[]> searchFieldsColumns) {
        this.searchFieldsColumns = searchFieldsColumns;
    }

    public ArrayList<JComboBox> getFilterComboBoxes() {
        return filterComboBoxes;
    }

    public void setFilterComboBoxes(ArrayList<JComboBox> filterComboBoxes) {
        this.filterComboBoxes = filterComboBoxes;
    }

    public ArrayList<int[]> getFilterComboBoxesColumns() {
        return filterComboBoxesColumns;
    }

    public void setFilterComboBoxesColumns(ArrayList<int[]> filterComboBoxesColumns) {
        this.filterComboBoxesColumns = filterComboBoxesColumns;
    }

    @Override
    public void sorterChanged(RowSorterEvent e) {
        if(e.getType() == RowSorterEvent.Type.SORT_ORDER_CHANGED){
            System.out.println("1");
                DefaultTableModel model = (DefaultTableModel) table.getModel();
                model.setRowCount(0);
        }
        if (e.getType() == RowSorterEvent.Type.SORTED){
            System.out.println("2");
            
        }
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        System.out.println("Hello World");
    }
}
